Dart RegExp operator ==
Syntax & Examples
RegExp.operator == operator
The equality operator `==` with Dart's `RegExp` objects as operands compares the two RegExp objects for equality.
Syntax of RegExp.operator ==
The syntax of RegExp.operator == operator is:
operator ==(dynamic other) → boolThis operator == operator of RegExp the equality operator.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
other | required | The object to compare for equality with this object. |
✐ Examples
1 Equality of Two Identical RegExp Objects
In this example,
- We create two identical regular expressions `regex1` and `regex2` with the pattern 'hello'.
- We compare them using the equality operator `==`.
Dart Program
void main() {
RegExp regex1 = RegExp('hello');
RegExp regex2 = RegExp('hello');
print('regex1 == regex2: ${regex1 == regex2}');
}Output
regex1 == regex2: true
2 Inequality of Two Different RegExp Objects
In this example,
- We create two different regular expressions `regex1` and `regex2` with different patterns.
- We compare them using the equality operator `==`.
Dart Program
void main() {
RegExp regex1 = RegExp('hello');
RegExp regex2 = RegExp('world');
print('regex1 == regex2: ${regex1 == regex2}');
}Output
regex1 == regex2: false
3 Equality of Two Identical RegExp Objects with Complex Pattern
In this example,
- We create two identical regular expressions `regex1` and `regex2` with the pattern '\d+'.
- We compare them using the equality operator `==`.
Dart Program
void main() {
RegExp regex1 = RegExp(r'\d+');
RegExp regex2 = RegExp(r'\d+');
print('regex1 == regex2: ${regex1 == regex2}');
}Output
regex1 == regex2: true
Summary
In this Dart tutorial, we learned about operator == operator of RegExp: the syntax and few working examples with output and detailed explanation for each example.